package org.cfk.delaytasktutorial.controller;

import jakarta.annotation.Resource;
import lombok.Data;
import org.cfk.delaytasktutorial.bo.XxlJobBo;
import org.cfk.delaytasktutorial.enums.LazyJobLazyLevel;
import org.cfk.delaytasktutorial.example.CacheCleanupTask;
import org.cfk.delaytasktutorial.example.NotificationTask;
import org.cfk.delaytasktutorial.example.OrderTimeoutTask;
import org.cfk.delaytasktutorial.service.LazyJobService;
import org.cfk.delaytasktutorial.service.XxlJobService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/api/lazy-job")
public class LazyJobController {

    @Resource
    private LazyJobService lazyJobService;

    @Resource
    private XxlJobService xxlJobService;

    @PostMapping("/notification")
    public void addNotificationTask(@RequestBody NotificationTask.NotificationParam param) {
        lazyJobService.add(NotificationTask.WORKER_NAME,
                cn.hutool.json.JSONUtil.toJsonStr(param),
                LocalDateTime.now().plusSeconds(5),
                LazyJobLazyLevel.MINUTE5);
    }

    @PostMapping("/order-timeout")
    public void addOrderTimeoutTask(@RequestBody OrderTimeoutTask.OrderTimeoutParam param) {
        lazyJobService.add(OrderTimeoutTask.WORKER_NAME,
                cn.hutool.json.JSONUtil.toJsonStr(param),
                LocalDateTime.now().plusMinutes(6),
                LazyJobLazyLevel.MINUTE5);
    }

    @PostMapping("/cache-cleanup")
    public void addCacheCleanupTask(@RequestBody CacheCleanupTask.CacheCleanupParam param) {
        lazyJobService.add(CacheCleanupTask.WORKER_NAME,
                cn.hutool.json.JSONUtil.toJsonStr(param),
                LocalDateTime.now().plusMinutes(100),
                LazyJobLazyLevel.MINUTE5);
    }

    @PostMapping("/xxljob")
    public void exeXxlJob() {
        XxlJobBo xxlJobBo = new XxlJobBo();
        xxlJobBo.setHandler("lazyJobExecutor");
        xxlJobBo.setParams("");
        xxlJobService.executeXxlJob(xxlJobBo);
    }
}